package com.bw.a2207a.view.fragment

import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bw.a2207a.R
import com.bw.a2207a.base.BaseFragment
import com.bw.a2207a.bean.TypeBean
import com.bw.a2207a.bean.TypeItemBean
import com.bw.a2207a.databinding.FragmentHomeBinding
import com.bw.a2207a.databinding.FragmentTypeBinding
import com.bw.a2207a.view.adapter.TypeLeftAdapter
import com.bw.a2207a.view.adapter.TypeRightAdapter
import com.bw.a2207a.viewmodel.LoginViewModel
import com.bw.a2207a.viewmodel.TypeViewModel
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter.OnItemClickListener

/**
 * create an instance of this fragment.
 */
class TypeFragment : BaseFragment<FragmentTypeBinding, TypeViewModel>(R.layout.fragment_type) {
    lateinit var adapter: TypeLeftAdapter
    lateinit var adapterRight: TypeRightAdapter
    val datas = mutableListOf<TypeBean>()
    val dataRight = mutableListOf<TypeItemBean>()
    override fun initData() {
        viewModel.typeLiveData.observe(this) {
            if (it.code == 200) {
                datas.clear()
                datas.addAll(it.data)
                adapter.notifyDataSetChanged()

                //给二级列表设置默认数据，
                dataRight.clear()
                if (datas.size > 0) {
                    //默认选中第一条数据
                    datas[0].isChecked = true

                    dataRight.addAll(datas[0].data)
                    adapterRight.notifyDataSetChanged()
                }
            }
        }
    }

    override fun initView() {

        //获取分类列表
        viewModel.getType()

        //左侧分类列表
        binding.rvLeft.layoutManager = LinearLayoutManager(activity)
        adapter = TypeLeftAdapter(activity, R.layout.item_type_left_layout, datas)
        binding.rvLeft.adapter = adapter

        //右侧列表
        binding.rvRight.layoutManager = GridLayoutManager(activity, 3)
        adapterRight = TypeRightAdapter(R.layout.item_type_right_layout, dataRight)
        binding.rvRight.adapter = adapterRight

        //处理左侧列表item点击事件
        adapter.setOnItemClickListener(object : OnItemClickListener {
            override fun onItemClick(p0: View?, p1: RecyclerView.ViewHolder?, p2: Int) {
                //单选逻辑；先把所以数据设置为未选中
                datas.forEach { it.isChecked = false }
                //再设置 当前点击条目为选中状态
                datas.get(p2).isChecked = true

                binding.tvName.text = datas.get(p2).category_name
                adapter.notifyDataSetChanged()

                //展示 item.data二级列表的具体数据
                //给dataRight 赋值
                dataRight.clear()
                dataRight.addAll(datas.get(p2).data)

                adapterRight.notifyDataSetChanged()

            }

            override fun onItemLongClick(
                p0: View?,
                p1: RecyclerView.ViewHolder?,
                p2: Int
            ): Boolean {
                return false
            }

        })

        //右侧列表


    }


}